<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\api\controller\v1\order;

use app\api\controller\BaseController;
use app\api\validate\v1\order\OrderAddressValidate;
use app\Request;
use app\services\order\OrderAddressService;

/**
 * 订单地址相关
 */
class OrderAddressController extends BaseController
{
    protected $service;

    public function __construct()
    {
        $this->service = new OrderAddressService();
    }
    /**
     * 订单地址列表
     */
    public function index(Request $request)
    {
        $params['user_id'] = $request->uid();

        $result = $this->service->index($params);

        return app('json')->success($result);
    }

    /**
     * 订单地址添加
     */
    public function add(Request $request)
    {
        $params = $request->postMore([
            ['real_name', ''],
            ['user_phone', ''],
            ['district', ''],
            ['detail', ''],
            ['post_code', ''],
            ['email', ''],
            ['is_default', ''],
            ['city_id', ''],
            ['city', ''],
            ['district', ''],
            ['province', ''],
            ['phone', '']
        ]);
        $params['uid'] = $request->uid();
        $this->baseValidate((new OrderAddressValidate()), $request->action(), $params);
        $this->service->add($params);

        return app('json')->success(100010);
    }

    /**
     * 订单地址详情
     */
    public function view(Request $request)
    {
        $params = $request->getMore([
            ['address_id', ''],
        ]);
        $this->baseValidate((new OrderAddressValidate()), $request->action(), $params);
        $result = $this->service->view($params);

        return app('json')->success($result);
    }

    /**
     * 订单地址编辑
     */
    public function edit(Request $request)
    {
        $params = $request->postMore([
            ['address_id', ''],
            ['real_name', ''],
            ['user_phone', ''],
            ['district', ''],
            ['detail', ''],
            ['post_code', ''],
            ['email', ''],
            ['is_default', ''],
            ['city_id', ''],
            ['city', ''],
            ['district', ''],
            ['province', ''],
            ['order_id', ''],
            ['phone', '']
        ]);
        $params['id'] = $params['address_id'];
        $params['uid'] = $request->uid();
        $this->baseValidate((new OrderAddressValidate()), $request->action(), $params);
        $this->service->edit($params);

        return app('json')->success(100010);
    }

    /**
     * 订单地址删除
     */
    public function delete(Request $request)
    {
        $params = $request->postMore([
            ['address_id', ''],
        ]);
        $this->baseValidate((new OrderAddressValidate()), $request->action(), $params);
        $this->service->delete($params);

        return app('json')->success(100010);
    }

}